<?php
include("template_top.php");
if(isset($_POST['building'])){
	$_POST['building'] = strtoupper($_POST['building']);
	require("HelpdeskTicket.php");
	$ticket = new HelpdeskTicket;
	$description = $_POST['problem']."\n \n"."Dispatched ".$_POST['tech'].".\n \n"."Ticket submitted by ".$USER['sFname']." ".$USER['sLname']." in the Technology Support Center on the Lee Campus.";
	if(!$ticket->SendClassroomDown($description, $_POST['building']."-".$_POST['room'])){
		$_GET['err'] = "An error occurred while sending the ticket information to Parature.<br/><br/>The information has been email to the developers and they will be sure to submit it as soon as they fix the problem.";
		$error = $ticket->getLastErrorString();
		$subject = "Portal Error";
		$body = "The following error occured while trying to send classroom down information to Parature.  The submission info follows.<br/>=======================================================<br/>Classroom Down Building-Room: ".$_POST['building']."-".$_POST['room']."<br/><br/>Classroom Down Problem: ".$_POST['problem']."<br/><br/>Error Returned: ".$error."<br/><br/>Referring User: ".$USER['sFname']." ".$USER['sLname']."<br/>=======================================================<br/><br/>This error has been sent to all administrators.";
		sendEmail('kpeterson5@edison.edu', $subject, $body);
	}else{
		customerSignIn($_POST['building']."-".$_POST['room'], "Classroom Down");		
		$sql = "INSERT INTO `tbTickets` (iID, sTech, sBuilding, sRoom, sProblem, sDispatched, sTime, sDate, sCaller) VALUES (null, :tech, :building, :room, :problem, :dispatched, :time, :date, :caller)";
		db::Query($sql, array(':tech'=>$USER['sFname']." ".$USER['sLname'], ':building'=>$_POST['building'], ':room'=>$_POST['room'], ':problem'=>$_POST['problem'], ':dispatched'=>$_POST['tech'], ':time'=>getCurrentTime(), ':date'=>getCurrentDate(), ':caller'=>$_POST['caller']));		
		$sql = "UPDATE `tbTechs` SET `sLastCall` = :time, `sLastCallBuilding` = :building WHERE `sTech` = :tech";
		db::Query($sql, array(':time'=>time(), ':building'=>$_POST['building'], ':tech'=>$_POST['tech']));
		addActivity($_POST['tech']." dispatched to ".$_POST['building']."-".$_POST['room']);
		$subject = $_POST['building']."-".$_POST['room']." Classroom Down";
		$body = "The following classroom down has been submitted.  The submission info follows.<br/>=======================================================<br/>Classroom Down Building-Room: ".$_POST['building']."-".$_POST['room']."<br/><br/>Classroom Down Problem: ".$_POST['problem']."<br/><br/>Technician Dispatched: ".$_POST['tech']."<br/><br/>Submission User: ".$USER['sFname']." ".$USER['sLname']."<br/>=======================================================<br/><br/>This error has been sent to all administrators.";
		//sendEmail('kpeterson5@edison.edu', $subject, $body);
		$_GET['msg'] = "Classroom Down Submitted Successfully";
		if($newyear){
			$sql = "SELECT * FROM `tbHolidayLog` WHERE `sUser` = :user";
			$q = db::Query($sql, array(':user'=>$USER['sUname']));
			$row = $q->fetch();
			if($row['iCount'] == ''){
				$sql = "INSERT INTO `tbHolidayLog` VALUES(:user, 0)";
				db::Query($sql, array(':user'=>$USER['sUname']));
				$row['iCount'] = 0;
			}
			$row['iCount'] ++;
			$sql = "UPDATE `tbHolidayLog` SET `iCount` = :count WHERE `sUser` = :user";
			db::Query($sql, array(':user'=>$USER['sUname'], ':count'=>$row['iCount']));
		}
	}
}
?>
<h1>Classroom Down Support Center</h1>
<p>
<h4>Submit A Classroom Down</h4>
<form action="Classroom_Down_Center.php" method="POST">
<table width="100%" cellpadding="0" cellspacing="5">
	<tr>
    	<td width="200" align="right">
			Building Letter:
        </td>
        <td>
        	<input type="text" name="building" style="width:97%" />
        </td>
    </tr>
    <tr>
    	<td width="200" align="right">
        	Room Number:
        </td>
        <td>
			<input type="text" name="room" style="width:97%" />
        </td>
    </tr>
    <tr>
    	<td width="200" valign="top" align="right">
        	Suggested Desktop Technician:
        </td>
        <td>
        	<?php
			$sql = "SELECT * FROM `tbTechs` ORDER BY `sTech` ASC";
			$q = db::Query($sql);
			$techs = array();
			$suggest = "";
			while($row = $q->fetch()){
				$avaliable = true;
				if($row['sHours'.date("D")] == ""){
					$avaliable = false;
				}else{
					$temp = explode("-", $row['sHours'.date("D")]);
					$sStart = $temp[0];
					$sEnd = $temp[1];
					$sStart = strtotime($sStart.":00 ".date("M j, Y"));
					$sEnd = strtotime($sEnd.":00 ".date("M j, Y"));
					if($sStart < time()){
						if($sEnd < time()){
							$avaliable = false;
						}
					}else{
						$avaliable = false;
					}
				}
				if($row['sOutTill'] != ""){
					if(time() < $row['sOutTill']){
						$avaliable = false;
					}
				}
				if($row['sProjectTill'] != ""){
					if(time() < $row['sOutTill']){
						$avaliable = false;
					}
				}
				if($row['sTech'] == "Kody Peterson"){
					$avaliable = false;
				}
				if($row['sTech'] == "Sam Flecha"){
					$avaliable = false;
				}
				if($row['sTech'] == "Tyla Bebon"){
					$avaliable = false;
				}
				if($avaliable){
					//if((time() - $row['sLastCall']) > 899){
						$techs[] = $row['sTech'];
					//}
				}else{
					$techs2[] = $row['sTech'];
				}
			}
			$suggest = rand(0, count($techs) - 1);
			?>
            <?php if(count($techs) == 0){ ?>
            There are currently no techs avaliable for call. 
            <?php }else{ ?>
        	<span id="suggestion"><?php echo $techs[$suggest] ?> - <a href="" onclick="document.getElementById('suggestion').style.display = 'none'; document.getElementById('techlist').style.display = 'block'; document.getElementById('hidden_tech').value = ''; return false;">change</a> - <font color="#FF0000">Please Try The Suggested Tech FIRST</font></span>
            <select id="techlist" style="display:none; width:97%" onchange="document.getElementById('hidden_tech').value = this.value;">
            <option value="">-- SELECT A TECH --</option>
            <?php foreach($techs as $value){ ?>
            	<option value="<?php echo $value ?>"><?php echo $value ?></option>
            <?php } ?>
            <option value="">-- TECHS NOT ON CALL --</option>
            <?php foreach($techs2 as $value){ ?>
            	<option value="<?php echo $value ?>"><?php echo $value ?></option>
            <?php } ?>
            <option value="Dean Phetterplace">Dean Phetterplace</option>
            </select>
            <?php } ?>
        </td>
    </tr>
    <tr>
    	<td width="200" align="right">
        	Last Tech Called:
        </td>
        <td>
        	<?php
			$sql = "SELECT * FROM `tbTechs` WHERE `sTech` != 'Tyla Bebon' ORDER BY `sLastCall` DESC";
			$q = db::Query($sql);
			$row = $q->fetch();
			echo $row['sTech']." - Building ".$row['sLastCallBuilding']." - ".date("n/j/Y @ g:i A", $row['sLastCall']);
			?>
        </td>
    </tr>
    <tr>
    	<td width="200" align="right">
        	Who is Calling In:
        </td>
        <td>
        	<input type="text" name="caller" style="width:97%" />
        </td>
    </tr>
    <tr>
    	<td valign="top" width="200" align="right">
        	Description of Problem:
        </td>
        <td>
        	<textarea name="problem" style="width:97%; height:100px"></textarea>
        </td>
    </tr>
    <tr>
    	<td>
        	<input type="hidden" name="tech" value="<?php echo $techs[$suggest] ?>" id="hidden_tech" />
        </td>
        <td>
        	<input type="submit" value="Submit Classroom Down" onclick="if(document.getElementById('hidden_tech').value == ''){alert('You Must Select A Tech'); return false;}else{return confirm('Are you sure you would like to dispatch:\n\n'+document.getElementById('hidden_tech').value);}" />
        </td>
    </tr>
</table>
</form>
<br/><br/>
<h4>Last 10 Classroom Downs</h4>
<table width="100%">
	<tr>
		<td>Room</td>
		<td>Dispatched</td>
        <td>Submitted By</td>
        <td>Actions</td>
	</tr>
<?php
$sql = "SELECT * FROM `tbTickets` ORDER BY `iID` DESC LIMIT 0, 10";
$q = db::Query($sql);
$c = 1;
while($row = $q->fetch()){
?>
	<tr>
		<td><?php echo $row['sBuilding']."-".$row['sRoom'] ?></td>
		<td><?php echo $row['sDispatched'] ?></td>
        <td><?php echo $row['sTech'] ?></td>
        <td><a href="#" onclick="alert('Classroom Down Details\n\nDate: <?php echo $row['sDate'] ?>\nTime: <?php echo $row['sTime'] ?>\nLocation: <?php echo $row['sBuilding']."-".$row['sRoom'] ?>\nDispatched: <?php echo $row['sDispatched'] ?>\nSubmitted By: <?php echo $row['sTech'] ?>\nProblem: <?php echo $row['sProblem'] ?>'); return false;">View Details</a></td>
    </tr>
<?php 
$c ++;
} ?>
</table>
<?php
include("template_bot.php");
?>